package gortepfeil;

import java.util.ArrayList;

/**
 * This class represents a polyline.
 * A polyline consists of lines which are connected.
 * @author Nikolai Gorte, Matthias Pfeil
 *
 */
public class Polyline
{
	/**
	 * ArrayList for single lines
	 */
	private ArrayList<Line> linesOfPolyline;

	/**
	 * Gets the lines of the polyline
	 * @return arrayslist with lines of the polyline
	 */
	public ArrayList<Line> getLines()
	{
		return linesOfPolyline;
	}

	/**
	 * Constructor
	 */
	public Polyline ()
	{
		this.linesOfPolyline = new ArrayList<Line>();
	}
	
	/**
	 * Adds a line to a polyline
	 * @param line line which should be added
	 * @throws PolylineException is thrown when the startpoint is not the same point like
	 * 		   the end point of the predecessor line 
	 */
	public void addLine(Line line) throws PolylineException
	{
		Line predecessorLine = null; 
		
		if (this.linesOfPolyline.size() > 0)
		{
			predecessorLine = this.linesOfPolyline.get(this.linesOfPolyline.size() - 1);	
		}
		else
		{
			this.linesOfPolyline.add(line);
			return;
		}
		
		if (predecessorLine.getEndPoint().isEqualToPoint(line.getStartPoint())) 
		{
			this.linesOfPolyline.add(line);
		}
		else
		{
			PolylineException pE = new PolylineException("File does not contain a polyline!");
			throw pE;
		}
	}
	
	/**
	 * Gets the length of a polyline
	 * @return length of the polyline
	 */
	public double getLength()
    {
            double length = 0.0;
            
            for (int i = 0; i < linesOfPolyline.size(); i++) 
            {
                    length = length + linesOfPolyline.get(i).getLength();
            }
            
            return length;
    }
	
	@Override
	public String toString() 
	{
		String result = "";
		
		for (Line line : this.linesOfPolyline) 
		{
			result = result + line + " -> ";
			
		}
		
		return result;
	}
}
